package com.tanhua.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.CommentApi;
import com.tanhua.api.mongo.PublishApi;
import com.tanhua.api.mongo.VideoApi;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mogo.Comment;
import com.tanhua.domain.mogo.Publish;
import com.tanhua.domain.mogo.Video;
import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.PublishVo;
import com.tanhua.domain.vo.VideoVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private VideoApi videoApi;

    @Reference
    private PublishApi publishApi;

    @Reference
    private CommentApi commentApi;

    //分页查询所有用户数据
    public ResponseEntity findUsersByPage(Integer page, Integer pagesize) {

        //mybatisPlush分页插件做查询
        IPage ipage=userInfoApi.findUsersByPage(page,pagesize);
        PageBeanVo pageBeanVo = new PageBeanVo(page,pagesize,ipage.getTotal(),ipage.getRecords());
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findUserById(Long userId) {
        UserInfo userInfo = userInfoApi.findUserInfo(userId);
        return ResponseEntity.ok(userInfo);
    }


    //查询单个用户视频数据
    public ResponseEntity findUserVideo(Integer page, Integer pagesize, Long uid) {

        // 分页查询该用户发布了哪些视频
        PageBeanVo pb=videoApi.findUserVideo(page,pagesize,uid);
        List<Video> items = (List<Video>) pb.getItems();
        // 查询userInfo的数据
        List<VideoVo> videoVoList=new ArrayList<>();
        if(CollectionUtil.isNotEmpty(items)){
            for (Video video : items) {
                UserInfo userInfo = userInfoApi.findUserInfo(video.getUserId());
                // 封装数据到videoVo中
                VideoVo videoVo = new VideoVo();
                BeanUtils.copyProperties(userInfo,videoVo);
                BeanUtils.copyProperties(video,videoVo);
                videoVo.setId(video.getId().toString());
                videoVo.setSignature(video.getText());
                videoVo.setCover(video.getPicUrl());
                videoVoList.add(videoVo);
            }
        }
        // 返回pageBean
        pb.setItems(videoVoList);


        return ResponseEntity.ok(pb);
    }

    //查询动态数据
    public ResponseEntity findUserPublish(Integer page, Integer pagesize, Long uid, Integer state) {

        PageBeanVo pb=publishApi.findUserPublish(page,pagesize,uid,state);
        List<Publish> items = (List<Publish>) pb.getItems();

        List<PublishVo> voList=new ArrayList<>();
        if(CollectionUtil.isNotEmpty(items)) {
            for (Publish publish : items) {
                //userInfo
                UserInfo userInfo = userInfoApi.findUserInfo(publish.getUserId());
                //publishVo
                PublishVo vo = new PublishVo();
                vo.setUserInfo(userInfo);
                vo.setPublish(publish);
                vo.setId(publish.getId().toString());
                vo.setCreateDate(new DateTime(publish.getCreated()).toString("yyyy-MM-dd HH:mm"));
                voList.add(vo);
            }
        }
        pb.setItems(voList);
        return ResponseEntity.ok(pb);
    }

    public ResponseEntity findPublishById(String publishId) {
        Publish publish = publishApi.findById(publishId);
        UserInfo userInfo = userInfoApi.findUserInfo(publish.getUserId());
        PublishVo vo = new PublishVo();
        vo.setUserInfo(userInfo);
        vo.setPublish(publish);
        vo.setId(publish.getId().toString());
        vo.setCreateDate(new DateTime(publish.getCreated()).toString("yyyy-MM-dd HH:mm"));

        return ResponseEntity.ok(vo);
    }



    public ResponseEntity findComment(Integer page, Integer pagesize, String publishId) {

        PageBeanVo pageBeanVo = commentApi.findComments(publishId, page, pagesize);
        List<Comment> items = (List<Comment>) pageBeanVo.getItems();

        List<CommentVo> commentVoList=new ArrayList<>();
        if(CollectionUtil.isNotEmpty(items)){
            for (Comment comment : items) {
                //userInfo
                UserInfo userInfo = userInfoApi.findUserInfo(comment.getUserId());
                //commentvo
                CommentVo vo = new CommentVo();
                BeanUtils.copyProperties(userInfo,vo);
                BeanUtils.copyProperties(comment,vo);
                vo.setId(comment.getId().toString());
                commentVoList.add(vo);
            }
        }
        pageBeanVo.setItems(commentVoList);
        return ResponseEntity.ok(pageBeanVo);
    }
}
